using System;
using System.Data;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace BaseClasses
{
    /// <summary>
    /// Custom base page used for all web forms.
    /// </summary>
    public class BasePage : Page
    {
        protected override void InitializeCulture()
        {
            //retrieve culture information from session
            string culture = Convert.ToString(Session["culture"]);

            //check whether a culture is stored in the session
            if (culture.Length > 0) Culture = culture;

            //set culture to current thread
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

            //call base class
            base.InitializeCulture();
        }


        protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            EnableAddButton();
        }

        protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
            DisableAddButton();
            DisableInsetDiv();
        }

        protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DisableInsetDiv();
        }

        protected void LinqDataSource_Updated(object sender, LinqDataSourceStatusEventArgs e)
        {
            EnableAddButton();
            if (e.Exception == null)
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "UpdateSuccess").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "UpdateError").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Red;
                e.ExceptionHandled = true;
            }
        }

        protected void LinqDataSource_Deleted(object sender, LinqDataSourceStatusEventArgs e)
        {
            if (e.Exception == null)
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "DeleteSuccess").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "DeleteError").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Red;
                e.ExceptionHandled = true;
            }
        }

        protected void LinqDataSource_Inserted(object sender, LinqDataSourceStatusEventArgs e)
        {
            if (e.Exception == null)
            {
                SetControlsAfterInsert();
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "InsertSuccess").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "InsertError").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Red;
                e.ExceptionHandled = true;
            }
        }

        protected void LinqDataSource_Inserting(object sender, LinqDataSourceInsertEventArgs e)
        {
            SetControlsBrforeInsert(e);
            
        }

        protected void InsertDV_ItemCommand(object sender, DetailsViewCommandEventArgs e)
        {
            if (e.CommandName == "Cancel")
                DisableInsetDiv();
        }

        protected void AddNew_Click(object sender, EventArgs e)
        {
            EnableInsetDiv();
        }


        protected virtual void EnableAddButton() { }
        protected virtual void DisableAddButton() { }
        protected virtual void EnableInsetDiv() { }
        protected virtual void DisableInsetDiv() { }
        protected virtual void SetControlsAfterInsert() { }
        protected virtual void SetControlsBrforeInsert(LinqDataSourceInsertEventArgs e) { }

    }
}
